/** 
* 文 件 名:ImageUtils.java
* Copyright (C) 2007-2012:杭州希禾信息技术有限公司 版权所有 
* 创 建 人:胡浩
* 日    期:2012-3-19 下午9:18:49
* 修 改 人: 
* 日   期:
* 修改描述:
* 描   述:TODO
*/
package net.seehealth.healthpad.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

/**
 * @className ImageUtils
 * @author 胡浩
 * @description 常用工具类
 * @date 2012-3-19 下午9:18:49
 */
public class ImageUtils {
	
	final static int SIZE = 150;
	
	/**
	 * 到URL去下载图片回传BITMAP回来
	 * 
	 * @param imgUrl
	 * @return
	 */
	public  static Bitmap getBitmapFromUrl(String imgUrl) {
		URL url;
		Bitmap bitmap = null;
		try {
			url = new URL(imgUrl);
			InputStream is = url.openConnection().getInputStream();
			BufferedInputStream bis = new BufferedInputStream(is, 8192);
			byte[] bt = getBytes(bis);
			bitmap = bytes2Bitmap(bt);
			bis.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return bitmap;
	}
	
	/**
	 * 根据输入流获得字节数组
	 * @param is 输入流
	 * @return 字节数组
	 * @throws IOException 
	 */
	static byte[] getBytes(InputStream is) throws IOException {           
		ByteArrayOutputStream baos = new ByteArrayOutputStream();           
		byte[] b = new byte[1024];           
		int len = 0;           
		while ((len = is.read(b, 0, 1024)) != -1) {            
			baos.write(b, 0, len);           
			baos.flush();           
		}           
		byte[] bytes = baos.toByteArray();           
		return bytes;        
	}
	
	/**
	 * Bitmap 转 byte数组
	 * @param bm
	 * @return
	 */
	public static byte[] bitmap2Bytes(Bitmap bm){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
	 	return baos.toByteArray();
	}
	
	/**
	 * 字节数组转Bitmap,并进行相应压缩
	 * @param b
	 * @return
	 */
	public static  Bitmap bytes2Bitmap(byte[] b){
		
	    if(b.length!=0) {
	    	BitmapFactory.Options opts = new BitmapFactory.Options();
	    	opts.inJustDecodeBounds = true;
	    	BitmapFactory.decodeByteArray(b, 0, b.length, opts);
	    	opts.inSampleSize = computeSampleSize(opts, -1, SIZE*SIZE);
	    	opts.inJustDecodeBounds = false;
	    	
	    	return BitmapFactory.decodeByteArray(b, 0, b.length, opts);
	    }
	    return null;
    }
	
	/**
	 * 计算出图片缩小的倍数
	 * @param options
	 * @param minSideLength
	 * @param maxNumOfPixels
	 * @return
	 */
	public static int computeSampleSize(BitmapFactory.Options options,

	        int minSideLength, int maxNumOfPixels) {

	    	int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels);
	    	int roundedSize;
	    	if (initialSize <= 8) {

	    		roundedSize = 1;
	    		while (roundedSize < initialSize) {
	    			roundedSize <<= 1;
	    		}
	    	} else {
	    		roundedSize = (initialSize + 7) / 8 * 8;
	    	}
	    return roundedSize;
	}
	
	/**
	 * 计算图片初始化大小
	 * @param options 
	 * @param minSideLength
	 * @param maxNumOfPixels 最大像素
	 * @return 返回尺寸大小
	 */
	private static int computeInitialSampleSize(
			BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) {
		    double w = options.outWidth;
		    double h = options.outHeight;
	
		    int lowerBound = (maxNumOfPixels == -1) ? 1 :
		            (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
		    int upperBound = (minSideLength == -1) ? SIZE :
		            (int) Math.min(Math.floor(w / minSideLength),
		            Math.floor(h / minSideLength));
	
		    if (upperBound < lowerBound) {
		        return lowerBound;
		    }
	
		    if ((maxNumOfPixels == -1) &&
		            (minSideLength == -1)) {
		        return 1;
		    } else if (minSideLength == -1) {
		        return lowerBound;
		    } else {
		        return upperBound;
		    }
	}

	
}
